import myBehaviorCheck from "wechat-validate"
let app =  getApp();


Page({
  behaviors:[myBehaviorCheck],
  rules:{
    mobel:[
      {required:true,message:"请输入手机号码"},
      {pattern:/^1[3-9]\d{9}$/,message:"请输入符合规则的手机号码"}
    ],
    verificationCode:[
      {required:true,message:"请输入验证码"},
    ]
  },
  data: {
    countDownVisible: false,
    timestr:5000,
    mobel:"",
    verificationCode:""
  },
  countDownChange(ev) {
    this.setData({
      timeData: ev.detail,
      countDownVisible: ev.detail.minutes === 1 || ev.detail.seconds > 0,
    })
  },
 async showerrst(){
  const isres =  this.checkprofile()
  if (!isres) return 

  this.setData({
    countDownVisible:true
  })


    const res = await wx.myRequest({
      url: "/code",
      data:{
        mobile:this.data.mobel
      }
    })

  },
  checkprofile(){
    const {valid,message} = this.validate('mobel')

    if (!valid){
      wx.utils.toast(message) 
      return false
    }else{
      return true
    }
    
   
  
  },
  onLoad(ev){
    this.redUrl = ev.redUrl
    this.reductType= ev.reductType
  },
  async loginfn(){

    const mobilecheck = this.validate('mobel')
    if (!mobilecheck.valid)  return wx.utils.toast("请输入正确的手机号")

    const verificationCodeCheck  = this.validate('verificationCode')
    if (!verificationCodeCheck.valid) return wx.utils.toast("请输入正确的验证码")
    
    const res = await wx.myRequest.post("/login",{
        mobile:this.data.mobel,
        code:this.data.verificationCode
    })
    
    app.token = res.data.token
    app.refreshToken = res.data.refreshToken
    wx.setStorageSync('token', res.data.token)
    wx.setStorageSync("refreshToken",res.data.refreshToken)
    if (this.reductType === 'switchTab') {
      wx.switchTab({
        url: "/"+this.redUrl
      })
    }else{
      wx.redirectTo({
        url: "/"+this.redUrl
      })
    }
  
  }
})
